<?php

declare (strict_types=1);

namespace app\beautiful\controller\cashier;

use app\BaseController;
use app\beautiful\model\project\ProjectModel;
use app\beautiful\model\project\ProjectTypeModel;
use app\common\logic\DataLogic;

class Project extends BaseController
{
    protected $name = '项目';

    public function index()
    {
        $full_name = $this->request->param('full_name');
        $project_type_id = $this->request->param('project_type_id/d');
        $store_id = $this->request->param('store_id/d', 0);
        $project_id = $this->request->param('project_id/d', 0);
        $is_delete = $this->request->param('is_delete');
        $where = [
            ['shop_id', '=', $this->request->shopId],
        ];
        if ($is_delete !=''){
            $where[] = ['is_delete', '=', $is_delete];
        }

        $project_model = new ProjectModel();
        $project_model = $project_model->with(['type']);

        if (!empty($full_name)) $where[] = ['full_name', 'like', '%' . $full_name . '%'];
        if (!empty($project_type_id)) $where[] = ['project_type_id', '=', $project_type_id];
        if (!empty($project_id)) $where[] = ['project_id', '=', $project_id];
        if (!empty($store_id)) {
            $project_model = $project_model->where("FIND_IN_SET('" . $store_id . "', store) > 0");
        }
        $order = ['add_time' => 'desc'];
        $field = '*';
        $append = ['add_time_format'];
        $return = DataLogic::getDataList($project_model, $where, $order, $field, $this->request->limit, $this->request->page, $append);

        return $this->success($return);

    }

    public function getType()
    {
        $type_list = ProjectTypeModel::where([
            ['shop_id', '=', $this->request->shopId],
            ['is_delete', '=', 0]
        ])->field('project_type_id,name')->select()->toArray();
        return $this->success($type_list);
    }

}
